<template>
    <div class="income-expense" :class="[{'empty':showEmpty}]">
        <template v-if="!isLoadDb">
            <page-loading></page-loading>
        </template>
        <template v-else>
            <div v-if="showEmpty" class="div-empty">
                <span class="iconfont icon-zanwujilu"></span>
                <p class="empty-tip">当前没有记录哦~</p> 
            </div>
            <ul v-else class="cell-content" v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="20" infinite-scroll-immediate-check="false">
                <li v-for="dataItem in notesArr" :key="dataItem.ids">
                    <income-cell v-if="dataItem.type.toString()=='1'||dataItem.type.toString()=='2'" class="cell" :cell-data="dataItem"></income-cell>
                    <expense-cell v-else-if="dataItem.type.toString()=='3'" class="cell" :cell-data="dataItem"></expense-cell>
                </li>
            </ul>
        </template>
    </div>
</template>

<script>
import pageLoading from "../../loading/pageLoading.vue";
import { mapState } from "vuex";
import { formatDate } from "../../../common/js/date.js";
import incomeCell from "./incomeCell.vue";
import expenseCell from "./expenseCell.vue";
import Vue from "vue";
import { InfiniteScroll } from "mint-ui";
Vue.use(InfiniteScroll);

export default {
  name: "income-expense",
  data() {
    return {
      notesArr: [],
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      showEmpty: false,
      existIdArr: [], //保存已经显示的订单id
      isLoadDb: false
    };
  },
  components: {
    incomeCell,
    expenseCell,
    pageLoading
  },
  mounted() {
    this.showEmpty = false;
    this.requestData(false);
  },
  methods: {
    requestData(infinite = true) {
      if (infinite && this.curPage == 0) return;
      if (infinite && this.cellLoading) return;
      this.cellLoading = true;
      this.$http
        .post(this.path + `store/User/iaelist`, {
          uid: this.userid,
          page: this.curPage
        })
        .then(response => {
          this.setLoadDb();
          let serverData = response.body.result;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0) this.showEmpty = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            let detailsArr = this.analysisData(serverData);
            for (let i = 0; i < detailsArr.length; i++) {
              let orderid = detailsArr[i].ids;
              if (this.existIdArr.indexOf(orderid) >= 0) continue;
              this.notesArr.push(detailsArr[i]);
              this.existIdArr.push(orderid);
            }
          }
        });
    },
    analysisData(data) {
      let detailsArr = [];
      data.forEach((val, index, arr) => {
        let tempDate = new Date(val.opdate * 1000);
        let happenTime = formatDate(tempDate, "yyyy-MM-dd hh:mm");
        detailsArr.push({
          ids: val.ids,
          bossname: val.storename,
          username: val.clientname,
          money: val.nums,
          time: happenTime,
          type: val.usetype, //2代表分成记录、3代表提现记录
          goodName: val.goodsname
        });
      });
      return detailsArr;
    },
    setLoadDb() {
      if (this.isLoadDb) return;
      let clearId = setTimeout(() => {
        clearTimeout(clearId);
        this.isLoadDb = true;
      }, 200);
    }
  },
  computed: {
    ...mapState(["path", "userid", "role"])
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.income-expense {
  width: 100%;
  min-height: 100%;
  background-color: #efefef;

  &.empty {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    background-color: #fff;
  }

  .div-empty {
    margin-bottom 25vh;
    .icon-zanwujilu {
      font-size: 1.3rem;
      color: #595656;
    }

    .empty-tip {
      margin-top: 0.26rem;
      font-size: 0.13rem;
      text-align: center;
      color: #595656;
    }
  }

  .cell {
    background: #fff;
    margin-bottom: 0.09rem;
    // box-shadow: 0 1px 0px rgba(66, 73, 92, 0.2);
  }
}
</style>
